/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.api;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.AddAppReq;
import org.cshoo.tattoo.authorize.api.dto.FrontPermissionResp;
import org.cshoo.tattoo.authorize.api.dto.UpdateAppReq;
import org.cshoo.tattoo.authorize.app.AppAppService;
import org.cshoo.tattoo.authorize.domain.po.App;
import org.cshoo.tattoo.embed.api.dto.ValidResultResp;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/app")
@Slf4j
public class AppController {

    @Resource
    private AppAppService appAppService;

    @RequestMapping(value = "/queryCurrentUserFrontPermissions/{appCode}", method = RequestMethod.GET)
    public List<FrontPermissionResp> queryCurrentUserFrontPermissions(@PathVariable("appCode") String appCode) {
        return appAppService.queryCurrentUserFrontPermissions(appCode);
    }

    @RequestMapping(value = "/check/code", method = RequestMethod.GET)
    public ValidResultResp checkCodeAlreadyExists(@RequestParam String code) {
        log.info("code:{}", code);
        ValidResultResp result = new ValidResultResp();
        boolean exists = appAppService.checkAppCodeExists(code);
        result.setValid(!exists);
        return result;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Response<?> addApp(@RequestBody AddAppReq addAppReq) {
        log.info("addAppReq:{}", addAppReq);
        appAppService.addApp(addAppReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Response<?> updateApp(@RequestBody UpdateAppReq updateAppReq) {
        log.info("updateAppReq:{}", updateAppReq);
        appAppService.updateApp(updateAppReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Response<?> deleteApp(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        appAppService.deleteApp(id);
        return Response.builder().build();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<App> findAllApp() {
        log.info("findAllMicroservice");
        return appAppService.findAllApp();
    }
}
